What is hat?
The 'hat' npm package is a simple utility for generating unique identifiers (UUIDs). It is lightweight and easy to use, making it suitable for applications that require unique keys or tokens.
What are hat's main functionalities?
Generate a unique identifier
This feature allows you to generate a unique identifier using the 'hat' package. The generated ID is a string of random characters.
const hat = require('hat');
const id = hat();
console.log(id);
Generate a unique identifier with a specific bit length
This feature allows you to generate a unique identifier with a specified bit length. In this example, a 128-bit identifier is generated.
const hat = require('hat');
const id = hat(128);
console.log(id);
Generate a unique identifier with a custom base
This feature allows you to generate a unique identifier with a custom base using a rack. The rack can be used to generate multiple unique identifiers.
const hat = require('hat');
const rack = hat.rack();
const id = rack();
console.log(id);
Other packages similar to hat
uuid
The 'uuid' package is a popular library for generating RFC4122 UUIDs. It supports various versions of UUIDs (v1, v3, v4, v5) and is widely used in the Node.js ecosystem. Compared to 'hat', 'uuid' offers more standardized and widely recognized UUID formats.
nanoid
The 'nanoid' package is a small, secure, URL-friendly unique string ID generator. It is faster and smaller than 'uuid' and 'hat', making it suitable for performance-critical applications. 'nanoid' provides a customizable length and alphabet for the generated IDs.
shortid
The 'shortid' package is used to generate short, non-sequential, URL-friendly unique IDs. It is designed for use in environments where short, human-readable IDs are preferred. Compared to 'hat', 'shortid' focuses on generating shorter IDs that are easier to use in URLs.
hat
Generate random IDs and avoid collisions.
examples
hat
var hat = require('hat');
var id = hat();
console.log(id);
output:
0c82a54f22f775a3ed8b97b2dea74036
rack
var hat = require('hat');
var rack = hat.rack();
console.log(rack());
console.log(rack());
output:
1c24171393dc5de04ffcb21f1182ab28
fabe2323acc1b559dee43d4a1e16cbeb
methods
var hat = require('hat');
hat(bits=128, base=16)
Generate a random ID string with bits
of data in a base
.
Leading zeros are appended such that all outputs for a given number of bits have
equal length.
var rack = hat.rack(bits=128, base=16, expandBy)
Make a new hat rack. Call rack()
repeatedly to generate new IDs which are
checked for collisions.
If expandBy
is specified, increment bits
by this amount if too many
collisions occur. If expandBy
isn't specified, rack()
will throw if too many
collisions occur during generation.
Optionally call var id = rack(data)
to store data
at the new ID.
You can get the data out again with rack.get(id)
and set the data with
rack.set(id, value)
.